<?php

//KenDon::load('DB', 'api');

//class KD_Adapter_DB_PDO extends PDO implements KD_IDB{
class KD_Adapter_DB_PDO extends PDO {
    // <<method:__construct>>
      public function __construct($setting = NULL ){
        //*
        if( is_array($setting)  && array_key_exists('dsn', $setting) && array_key_exists( 'user', $setting) && array_key_exists('psw', $setting)  ){
            extract($setting);
            parent::__construct( $dsn, $user, $psw );
        }else{
            $_setting = array(
                    'dsn'       =>  '',
                    'host'      =>  'localhost',
                    'hostPort'  =>  3306,
                    'dbms'      =>  'mysql',
                    'dbName'    =>  '',
                    'user'      =>  'root',
                    'psw'       =>  '',
            );

            $setting = is_array($setting) ? array_merge($_setting, $setting) : $_setting ;

            extract($setting);

            switch($dbms){
              case 'mysql':
                  try{
                    $dsn = "mysql:dbname=$dbName;host=$host;";
                    parent::__construct($dsn, $user, $psw);
                  }catch(PDOException $e){
                    throw $e;
                  }
                  break;

               case 'sqlite':
                  try{
                    $dsn = 'sqlite:'. $dbName;
                    parent::__construct($dsn, null, null);
                  }catch( PDOException $e ){
                    throw $e;
                  }
                  break;

              case 'sqlite2':
                  $dsn = 'sqlite2:' . $dbName;
                  try{
                    parent::__construct($dsn, null, null);
                  }catch(PDOException $e){
                    throw $e;
                  }
                  break;  

               default:
                  throw new Exception('系统未支持此数据库类型');
                  break;
            }
        }
        //*/
      }
    // -- end -- <<method:__construct>>

    // <<method:query>>
      public function query($sql){
        $result = parent::query($sql);

        if($result === false){ return false;}

        $result->setFetchMode(PDO::FETCH_ASSOC);
        return $result;
      }
    // -- end -- <<method:query>>

    // <<method:execute>>
      public function execute($sql){
        parent::exec($sql);
      }
    // -- end -- <<method:execute>>

    // <<method:exec>>
      public function exec($sql){
        parent::exec($sql);
      }
    // -- end -- <<method:exec>>

    // <<method:startTrans>>
      public function startTrans(){
        parent::beginTransaction();
      }
    // -- end -- <<method:startTrans>>

    // <<method:commit>>
      public function commit(){
        parent::commit();
      }
    // -- end -- <<method:commit>>

    // <<method:rollback>>
      public function rollback(){
        parent::rollback();
      }
    // -- end -- <<method:rollback>>

}
